// pages/paper/paper.js
var app = getApp();
var ea = require('../../utils/each.js');
var time = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    getxiangqingUrl: app.data.url + 'zhongxue/teacher/shijuanxiangqing',
    getpingjiaUrl: app.data.url + 'zhongxue/teacher/getlaoshipingjia',
    tjpinglun: app.data.url + 'zhongxue/teacher/tijiaopinglun',
    xueshenghuifu: app.data.url + 'zhongxue/teacher/getxueshenghuifu',
    laoshihuifu: app.data.url +'zhongxue/teacher/laoshihuifu',
    id:0,
    shuju: {},
    imgUrl: [],
    pingjia:{},
    pjnr:{},
    huifu:{},
    shuliang:'',
    duration: 500,
    interval: 1000,

    paperUrl: [
      "/img/2.png"
    ],

    newDocument: [
      {
        url: "/img/6.png",
        name: "张老师",
        date: "2018-06-12",
        docu: "你的基础比较薄弱，特别是第二章没有理解清楚，需要课后多加练习，希望你能继续坚持下去！每天做半个小时的练习题，一定会有收获的！"
      },
      {
        url: "/img/6.png",
        name: "张老师",
        date: "2018-06-12",
        docu: "你的基础比较薄弱，特别是第二章没有理解清楚，需要课后多加练习，希望你能继续坚持下去！每天做半个小时的练习题，一定会有收获的！"
      }
    ]
  },
  

  Img:function(e){
    var index = e.currentTarget.dataset.index;
    console.log(e)
    var imgs=this.data.imgUrl
    wx.previewImage({
      current: imgs[index],
      urls: imgs,
    })
  },

  /**
   * 获取input框中的值
   */
  inputWacth:function(e){
    // console.log(e);
    let item = e.currentTarget.dataset.model;
    // console.log(item);
    var pjnr = this.data.pjnr;
    pjnr[item] = e.detail.value;
    // console.log(pjnr[item])
    this.setData({ pjnr: pjnr });
    this.setData({
      [item]: e.detail.value
    });
    console.log(pjnr)
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options)
    var id = options.id;
    this.setData({id:id});
    this.shuaxin();
  },

  shuaxin:function(){
    var that = this;
    // console.log(options);
    var user = wx.getStorageSync('newuser');
    // console.log(user)
    var id = this.data.id;
    var token = user.token;
    var shenhe = user.shenhe;
    var shuju = that.data.shuju;
    var imgUrl = that.data.imgUrl;
    var shuling = that.data.shuliang;
    var pingjia = that.data.pingjia;
    // var huifu = that.data.huifu;
    wx.request({
      url: that.data.getxiangqingUrl,
      method: 'post',
      data: { token: token, shenhe: shenhe, id: id },
      success: function (res) {
        var data = res.data;
        console.log(data)
        if (data.code == 200) {
          var shuju = data.data
          that.setData({ shuju: shuju })
          var imgUrl = data.data.testpaper;
          // console.log(imgUrl)
          that.setData({ imgUrl: imgUrl });
          var shuliang = data.data.testpaper.length;
          that.setData({ shuliang: shuliang });
        } else {
          wx.showToast({
            title: '获取失败',
            icon: 'none',
          })
        }
      }
    })

    wx.request({
      url: that.data.getpingjiaUrl,
      method: 'post',
      data: { token: token, shenhe: shenhe, id: id },
      success: function (res) {
        var data = res.data;
        // console.log(data);
        if (data.code == 200) {
          var pingjia = data.data
          // console.log(pingjia)
          that.setData({ pingjia: pingjia })
          // var huifu = data.data.huifu;
          console.log(pingjia)
        } else {
          wx.showToast({
            title: '获取失败',
            icon: 'none',
          })
        }
      }
    })

  },



  /**
   * 提交评论
   */
  pinglun: function () {
    var that = this;
    var pjnr = that.data.pjnr;
    var shijuan_id = that.data.shuju.id
    console.log(shijuan_id)
    var yanz = ea.yanzhen(pjnr, {
      dir: '请输入评论内容',
    })
    // console.log(yanz);
    if (yanz) {
      wx.showToast({
        title: yanz,
        icon: 'none'
      })
      return;
    }
    var user = wx.getStorageSync('newuser')
    var token = user.token
    var shenhe = user.shenhe
    pjnr.teacher_id = user.id;
    pjnr.shijuan_id = shijuan_id;
    pjnr.token = user.token;
    wx.request({
      url: that.data.tjpinglun,
      method:'post',
      data: pjnr,
      success:function(res){
        var data = res.data;
        console.log(data)
        // return
        if(data.code == 200){
          wx.showToast({
            title: '评论成功',
            icon: 'success',
          })
          that.shuaxin();
        }else{
          wx.showToast({
            title: '评论失败',
            icon: 'none',
          })

        }
      }
      
    })
  },

/**
 * 获取回复内容
 */
  huifuneirong:function(e){
    console.log(e);
    let item = e.currentTarget.dataset.model;
    var id = e.currentTarget.dataset.id;
    // console.log(item);
    var huifu = this.data.huifu;
    var user = wx.getStorageSync('newuser')
    var xingm = user.name;
    // console.log(huifu)
    huifu[item] = e.detail.value;
    huifu['name'] = xingm;
    huifu['pingjia_id'] = id;
    // console.log(pjnr[item])
    this.setData({ huifu: huifu });
    this.setData({
      [item]: e.detail.value
    });
    console.log(huifu)
  },


  /**
   * 提交回复
   */
  tijiaohuifu:function(){
    var that = this;
    var huifu = this.data.huifu;
    // console.log(id);
    // console.log(shuju);
    var yanz = ea.yanzhen(huifu, {
      dir: '请输入回复内容',
    })
    if (yanz) {
      wx.showToast({
        title: yanz,
        icon: 'none'
      })
      return;
    }
    var user = wx.getStorageSync('newuser')
    wx.request({
      url: that.data.laoshihuifu,
      method: 'post',
      data: huifu,
      success: function (res) {
        var data = res.data;
        // return
        if (data.code == 200) {
          wx.showToast({
            title: '回复成功',
            icon: 'success',
          })
          that.shuaxin();
        } else {
          wx.showToast({
            title: '回复失败',
            icon: 'none',
          })
        }
      }
    })


  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.shuaxin();
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})